var urlRoot = '../qajob/';

var fixed = false;


$(function() {
	getInfo();

	var fixData = getQueryString("fixed");
	if (fixData == 1)
		fixed = true;
});

var fData = {};

function getInfo() {
	jQuery.ajax({
		type: 'GET',
		contentType: 'application/json',
		url: urlRoot + 'info?debug=' + debug,
		dataType: 'json',
		async: false,
		success: function(data) {
			var model = "Unknown";
			if (checkApiErrorInfo(data, "获取模型信息出错")) {
				model = data.model;
			}

			if (data.info)
				log(data.info);

			fData = {};
			var files = data.files;
			var fItems = "";
			for (var i = 0; i < files.length; i++) {

				var f = files[i];
				fData[f.id] = f.content;

				fItems +=
					"<tr>\
						<td style='text-align: center'><button class='btn btn32 btn-file'></button></td>\
						<td style='text-align: left'>"+ f.name + "</td>\
						<td style='text-align: left'>" + f.len + "</td>\
						<td style='text-align: left'>" + f.size + "</td>\
						<td style='text-align: center'>\
							<button class='btn btn32 btn-up' onclick='useFile(" + f.id + ")'></button>\
						</td>\
					</tr>";
			}
			$("#fileTable").html(fItems);

			var dbg = data.debug;
			if (dbg) {
				$("#jumpButton").css("display", "block");
				$("#infoHead").css("display", "block");
				$("#infoHead").html("当前模型： " + model);
			}
			else {
				$("#jumpButton").css("display", "none");
				$("#infoHead").css("display", "none");
			}
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function useFile(id) {
	$('#textInput').val(fData[id]);
}

function talk() {

	var param = {}
	param.content = $('#textInput').val();

	jQuery.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: urlRoot + 'chat',
		dataType: 'json',
		async: false,
		data: JSON.stringify(param),
		success: function(data) {
			if (checkApiCodeReturn(data, "对话测试出错")) {
				$("#simpleoutput").html(data.content);
				$("costoutput").html("Talk check done. " + data.cost);
			}
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});

}

function parse(type) {

	var param = {}
	param.content = $('#textInput').val();
	param.num = $('#qaNum').val();
	param.type = type;
	param.fixed = fixed;
	param.dolog = true;

	jQuery.ajax({
		type: 'POST',
		contentType: 'application/json',
		url: urlRoot + 'parse',
		dataType: 'json',
		async: false,
		data: JSON.stringify(param),
		success: function(data) {
			if (checkApiCodeReturn(data, "问答对抽取出错")) {
				console.log(data);
				$("#simpleoutput").html(data.data.content);
				$("#costoutput").html("Content parse done. " + data.data.cost);
				log("");
			}
		},
		error: function(e) {
			log("返回结果出错： " + JSON.stringify(e));
		}
	});
}

function txtUpload() {

}